//
//  XYVisitorView.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/28.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit

class XYVisitorView: UIView {
    
    var visitorViewModel: XYVisitorViewModel? {
        
        didSet {
            
            iconView.image = UIImage(named: (visitorViewModel?.icon)!)
            textLabel.text = visitorViewModel?.text
            if visitorViewModel?.isHome == true {
                startAnimation()
            }else{
                circleView.hidden = true
            }
            
        }
    }
    // 动画
    private func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        // 切换界面是核心动画会停止 调用下面的方法可以使动画不被释放
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    // MARK: 懒加载控件
    lazy var registerBtn: UIButton = {
        
        let button = UIButton(title: "注册", image: "common_button_white_disable")
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return button
    }()
    lazy var loginBtn: UIButton = {
        
        let button = UIButton(title: "登录", image: "common_button_white_disable")
        
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        return button
    }()
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView()
        
        return imageView
    }()
    private lazy var circleView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return imageView
    }()
    private lazy var textLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.textAlignment = .Center
        return label
    }()
    private lazy var coverView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return imageView
    }()
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        addSubview(circleView)
        addSubview(coverView)
        addSubview(iconView)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(textLabel)
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: coverView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: textLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint((NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20)))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: textLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: 外界方法
    
    
}
